package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicLineResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 项目线路表控制器
 */
@Controller
@RequestMapping("/basicLine")
public class BasicLineController extends BaseController {

    private String PREFIX = "/basic/basicLine";

    @Autowired
    private BasicLineService basicLineService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicLine.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicLine_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicLine_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicLineParam basicLineParam) {
    	String lineName = basicLineParam.getLineName();
    	if (lineName!=null&&lineName!="") {
    		lineName = lineName.replace("& #40;", "(");
    		lineName = lineName.replace("& #41;", ")");
    		basicLineParam.setLineName(lineName);
    	}
    	Object warehouseOperateId = basicLineParam.getOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicLineParam.setOperateName(Operate.getOperateName());
    		basicLineParam.setDeliverId(Operate.getDeliverId());
    		basicLineParam.setDeptId(Operate.getDeptId());
		}
    	basicLineParam.setCreateTimes(DateUtil.getDateS());
    	basicLineParam.setCreateName(userService.getcurrentUser().getName());
        this.basicLineService.add(basicLineParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicLineParam basicLineParam) {
    	String lineName = basicLineParam.getLineName();
    	if (lineName!=null&&lineName!="") {
    		lineName = lineName.replace("& #40;", "(");
    		lineName = lineName.replace("& #41;", ")");
    		basicLineParam.setLineName(lineName);
    	}
    	Object warehouseOperateId = basicLineParam.getOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicLineParam.setOperateName(Operate.getOperateName());
		}
        this.basicLineService.update(basicLineParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Permission
    public ResponseData delete(BasicLineParam basicLineParam) {
        this.basicLineService.delete(basicLineParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicLineParam basicLineParam) {
        BasicLine detail = this.basicLineService.getById(basicLineParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicLineParam basicLineParam,String condition ) {
    	if (condition!=null&&condition!="") {
    		condition = condition.replace("& #40;", "(");
    		condition = condition.replace("& #41;", ")");
    	}
    	User user = userService.getcurrentUser();
    	basicLineParam.setDeliverId(user.getDeliverId());
    	basicLineParam.setDeptId(user.getDeptId());
        return this.basicLineService.findPageBySpec(basicLineParam,condition,user.getOperateId());
    }

    @ResponseBody
    @RequestMapping("/Querylist")
    public List<BasicLineResult> Querylist(Long  operateId) {
        return this.basicLineService.listByOperateId(operateId);
    }

}


